package com.pruebas.andengine;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.ZoomCamera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLayer;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLoader;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXProperties;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTile;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTileProperty;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLoader.ITMXTilePropertiesListener;
import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXLoadException;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.input.touch.detector.ScrollDetector;
import org.anddev.andengine.input.touch.detector.SurfaceScrollDetector;
import org.anddev.andengine.input.touch.detector.ScrollDetector.IScrollDetectorListener;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.Debug;

import android.util.Log;

import com.pruebas.andengine.CatapultDetector.ICatapultDetectorListener;

public class Main extends BaseGameActivity implements IScrollDetectorListener,
		IOnSceneTouchListener, ICatapultDetectorListener {
	// ===========================================================
	// Constants
	// ===========================================================
	static final int CAMERA_WIDTH = 480;
	static final int CAMERA_HEIGHT = 320;
	public static final float MAX_CHARGE_DISTANCE = 80;
	private static final String TAG = "AndEngineTest";

	// ===========================================================
	// Fields
	// ===========================================================

	private ZoomCamera mCamera;
	private Texture mTexture;
	private TiledTextureRegion mBallTextureRegion;
	private TiledTextureRegion mPlayerTextureRegion;
	private SurfaceScrollDetector mScrollDetector;
	private TMXTiledMap mTMXTiledMap;
	private Player mActivePlayer;
	private CatapultDetector mCatapultDetector; // Nueva variable Creada

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public void onLoadComplete() {
		createPlayer();
	}

	@Override
	public Engine onLoadEngine() {
		this.mCamera = new ZoomCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		final int alturaTotal = CAMERA_HEIGHT * 3;
		this.mCamera.setBounds(0, CAMERA_WIDTH, 0, alturaTotal);
		this.mCamera.setBoundsEnabled(true);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
				this.mCamera));
	}

	@Override
	public void onLoadResources() {
		this.mTexture = new Texture(256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mBallTextureRegion = TextureRegionFactory.createTiledFromAsset(
				this.mTexture, this, "gfx/ui_ball.png", 0, 0, 2, 4);
		this.mPlayerTextureRegion = TextureRegionFactory.createTiledFromAsset(
				this.mTexture, this, "gfx/ui_player.png", 128, 0, 2, 4);

		this.mEngine.getTextureManager().loadTexture(this.mTexture);

	}

	@Override
	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		final Scene scene = new Scene(1);

		try {
			final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine
					.getTextureManager(), // TextureOptions.BILINEAR_PREMULTIPLYALPHA,
					TextureOptions.NEAREST);
			this.mTMXTiledMap = tmxLoader.loadFromAsset(this, "tmx/field.tmx");
		} catch (final TMXLoadException tmxle) {
			Debug.e(tmxle);
		}

		final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
		scene.getFirstChild().attachChild(tmxLayer);

		scene.setOnAreaTouchTraversalFrontToBack();

		this.mScrollDetector = new SurfaceScrollDetector(this);
		this.mScrollDetector.setEnabled(false);

		this.mCatapultDetector = new CatapultDetector(MAX_CHARGE_DISTANCE, this);
		this.mCatapultDetector.setEnabled(true);

		scene.setOnSceneTouchListener(this);
		scene.setTouchAreaBindingEnabled(true);

		return scene;
	}

	@Override
	public void onCharge(CatapultDetector pCatapultDetector,
	TouchEvent pTouchEvent, float pDistance, float pAngle) {
		this.mActivePlayer.setCharge(pAngle, pDistance);
	}

	@Override
	public void onShoot(CatapultDetector pCatapultDetector,
			TouchEvent pTouchEvent, float pDistance, float pAngle) {
		Log.d(TAG, "Disparo... {Distancia:" + pDistance + ", angulo: " + pAngle
				+ "}");
	}

	@Override
	public void onScroll(ScrollDetector pScollDetector, TouchEvent pTouchEvent,
			float pDistanceX, float pDistanceY) {
		this.mCamera.offsetCenter(-pDistanceX, -pDistanceY);
	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		if (this.mActivePlayer != null) {
			this.mCatapultDetector.onTouchEvent(pSceneTouchEvent);
		}
		return true;
	}

	// ===========================================================
	// Methods
	// ===========================================================

	private void createPlayer() {
		final Scene scene = this.mEngine.getScene();

		final Player sprite = new Player(200, 100, this.mPlayerTextureRegion);
		scene.getLastChild().attachChild(sprite);

		this.mActivePlayer = sprite;
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
